<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="../templates/commons/common2 ::head(~{::title})">
    <meta charset="UTF-8" >
    <!--移动端更好的适应-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" th:href="@{/dist/semantic/semantic.min.css}">
    <link rel="stylesheet" th:href="@{/lib/editormd/css/editormd.min.css}">
    <link rel="stylesheet" th:href="@{/css/my.css}">
    <title>分类新增</title>
</head>
<body>
    <!--导航开始--------------------------------------------->
    <nav th:replace="../templates/commons/common2::menu(2)">
    </nav>
    <!-- 二级导航 -->

    <!--导航结束---------------------------------------------->


    <!--中间内容开始------------------------------------------>
    <div class="m-container-small m-padded-large">
        <div class="ui container">
            <form  method="post" class="ui form" th:id="typeform">
                <div class="field">
                    <div class="ui left labeled input">
                        <label class="ui teal basic label" >名称</label>
                        <input type="hidden" id="id" name="id" th:value="${type.id}">
                        <input type="text" id="name" name="name" th:value="${type.name}" placeholder="分类名称" >
                    </div>
                </div>



                <div class="ui error message"></div>

                <div class="ui right aligned container">
                    <button type="button" class="ui button" onclick="window.history.go(-1)">返回</button>
                    <button type="button" class="ui teal button" id="btn">提交</button>
                </div>

            </form>
        </div>
    </div>
    <!--中间内容结束------------------------------------------>
    <br>
    <!--底部内容开始-->
    <footer th:replace="../templates/commons/common2 :: footer">
    </footer>
    <!--底部内容结束------------------------------------------>

    <script th:replace="../templates/commons/common2 :: script"></script>

<script>

    /** 初始化editormd **/
    var contentEditor;
    $(function() {
        contentEditor = editormd("md-content", {
            width   : "100%",
            height  : 640,
            syncScrolling : "single",
            path    : "../static/lib/editormd/lib/"
        });
    });


    $('.m-menu.m-toggle').click(function () {
        $('.m-item').toggleClass('m-mobile-hide');
    });

    $('.ui.dropdown').dropdown({
        on : 'hover'
    });

    /*-- 表单验证 --*/
    $('.ui.form').form({
        fields : {
            title : {
                identifier: 'title',
                rules: [{
                    type : 'empty',
                    prompt : '标题：请博客标题'
                }]
            }
        }
    });

    $('#btn').on('click',function () {
        var id = $('#id').val();
        var type = $('#typeform').serialize();

        if (id==0){
            $.ajax({
                url: '/admin/inputType',
                type: 'post',
                data: type,
                dataType: 'text',
                success: function (data) {
                    window.alert(data);
                    if (data=="添加成功"){
                        location.href="/admin/type";
                    }
                },
                error:function (data) {
                    console.log(data);
                }
            });
        }else {
               $.ajax({
                   url:'/admin/updatetype',
                   type: 'post',
                   data:type,
                   dataType: 'text',
                   success:function (data) {
                        window.alert(data);
                        if (data == "修改成功"){
                            location.href="/admin/type";
                        }
                   }
               })
        }

    })






</script>
</body>
</html>